<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Novel;      //引入Novel类
use App\Chapter;    //引入Chapter类


class ChapterController extends Controller
{
    public function getChapteradd($nid){
      $novel = Novel::find($nid);
    	return view('admin/chapteradd' , ['novel' => $novel , 'nid' => $nid]);
    }

    public function postChapteradd(Request $req, $nid){      //章节添加


    	$chapter = new Chapter();     //新建一个章节对象
      $novel = Novel::find($nid);   //用于更新小说
      if ($novel->chnum == 0) {     //如果该小说没有章节，添加章节的前后都设定为0
        $chapter->pre = 0;
        $chapter->next = 0;
      } else {
        $lastchapter = Chapter::where('nid', $nid)->orderby('chid','desc')->first();//取出最后一章
        $chapter->pre = $lastchapter->chid;    //新建小说的前一章为最后一章的chid
        $chapter->next = 0;
      }

    	$chapter->nid = $nid;
    	$chapter->chtitle = $req->chtitle;
    	$chapter->content = $req->content;
    	$chapter->pubtime = time();
    	$chapter->edittime = time();

      $chapter->wordcount = mb_strlen($req->content);   //章节字数

    	$chapter->save();
      if($novel->chnum){
        $lastchapter->next = $chapter->chid;   //最后一章的next为最新一章的chid
        $lastchapter->save();
      }

      $novel->wordcount += $chapter->wordcount;
      $novel->chnum++;                                 //章节数加一
      $novel->edittime = $chapter->edittime;           //小说更新时间
      $novel->new = $chapter->chid;                    //更新小说最新章节
      $novel->save();

    	// return view('published');
      return redirect("published/$nid");
    }

    public function getChapteredit($nid,$chid){              //章节编辑
      $chapter = Chapter::find($chid);
      return view('admin/chapteredit' , ['chapter' => $chapter , 'nid' => $nid]);
    }

    public function postChapteredit(Request $req , $nid , $chid){  //章节编辑
      $data = Chapter::find($chid);
      $req = $req->all();
      $novel = Novel::find($nid);                         //用于更新小说字数

      $novel->wordcount -= $data->wordcount;              //先减掉该章节的字数

      $data->chtitle = $req['chtitle'];
      $data->content = $req['content'];
      $data->edittime = time();
      $data->wordcount = mb_strlen($req['content']);       //更新章节字数
      $data->save();

      $novel->wordcount += $data->wordcount;              //再加上编辑后的章节字数
      $novel->save();                                     //更新字数后入库

      return redirect("published/$nid");
    }

    public function getChapterdel($nid,$chid){            //章节删除
      $chapter = Chapter::find($chid);
      $novel = Novel::find($nid);                         //用于更新小说字数

      if ($chapter->next==0 && $novel->chnum>=2) {        //如果删除最后章节，且小说章节大于1
         $prechapter = Chapter::find($chapter->pre);     //取出上一章的信息
         $prechapter->next = 0;                          //上一章的next置为0
         $prechapter->save();
      }elseif ($chapter->pre==0 && $novel->chnum>=2) {     //如果删除第一章章节，且小说章节大于1
         $nextchapter = Chapter::find($chapter->next);     //取出下一章的信息
         $nextchapter->pre = 0;                            //上一章的next置为0
         $nextchapter->save();
      } elseif($novel->chnum>=3) {
         $prechapter = Chapter::find($chapter->pre);       //取出前一章和后一章
         $nextchapter = Chapter::find($chapter->next);
         $prechapter->next = $nextchapter->chid;           //前一章的next置为要删除的后一章的chid
         $nextchapter->pre = $prechapter->chid;            //后一章的next置为要删除的后一章的chid
         $prechapter->save();
         $nextchapter->save();
      }

      $novel->wordcount -= $chapter->wordcount;              //减掉该章节的字数
      $chapter->delete();                                    //删除章节
      $novel->chnum--;                                    //章节数减一
      $novel->save();                                     //更新字数后入库
      return redirect("published/$nid");

    }


    public function published($nid){                         //章节列表
    	$chapters = Chapter::where('nid', $nid)->orderby('chid')->paginate(3);    //取出章节信息
    	// dd($chapters);
      $novel = Novel::find($nid);   //取出小说名字
    	return view('admin/chapter-published', ['chapters'=>$chapters , 'nid'=>$nid , 'novel'=>$novel]);     //把数据传到chapter-published页面
    }

    public function getChapter($chid){               //小说阅读
      $chapter = Chapter::find($chid);

      $contents = explode('   ',$chapter->content);
      $novel = Novel::find($chapter->nid);
      return view('public/chapter',['chapter' => $chapter,'novel' => $novel,'contents'=>$contents]);
    }

}
